/**
 *Project: jh-framework
 *File: cn.xkshow.io.file.PropertFile.java 
 *Author: hardy(admin@xkshow.cn) Copyright
 ****************************************************************
 *   VERSION       DATE            BY       CHANGE/COMMENT
 * --------------------------------------------------------------
 *    1.0        2013-6-26         hardy         create
 ****************************************************************
 *note:
 ***************************************************************/ 
package cn.xkshow.io.file;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PropertFile {
	private final Log loger = LogFactory.getLog(this.getClass());
	private static PropertFile instance = null;
	
	private PropertFile(){
	}
	public static PropertFile getInstance(){
		if(null == instance){
			instance = new PropertFile();
		}
		return instance;
	}
	
	/**
	 * @param filePath
	 * Relative to the ROOT path class/...(file)
	 */
	public Properties loadingProperties(String filePath) {
        Properties props = null;  
        try {
            if (!filePath.equalsIgnoreCase("")) {             	
            	String confPath = PropertFile.class.getClassLoader().getResource(filePath).toString().replace("%20", " ").substring(6);
//            	System.out.println("PropertFile.loadingProperties 000:"+ confPath);
            	
            	InputStream  in = new BufferedInputStream(new FileInputStream(confPath));
            	props = new Properties(); 
    			props.load(in);
            }   
        } catch (Exception e){  
            loger.error("Read the resource file error:"+ e);   
        } finally { 
            return props;
        }
	}
	
	/**
	 * @param fileName
	 * Relative to the ROOT path class/...(file)
	 */
	public String retFilePath(String fileName) {
		String filepath = "";  
        try {
            if (!fileName.equalsIgnoreCase("")) {             	
            	filepath = PropertFile.class.getClassLoader().getResource(fileName).toString().replace("%20", " ").substring(6);
            }   
        } catch (Exception e){
            loger.error("Get file path anomalies: "+ e);   
        } finally { 
            return filepath;
        }
	}

	/** 
     * Modify or add key-value pairs : If the key exists , modify ; otherwise add .
     * @param filepath 
     * @param key 
     * @param value 
     */ 
    public void writeData(String filepath, String key, String value) {  
    	InputStream fis = null;
        Properties prop = null; 
        OutputStream fos = null;
        try {  
        	String confPath = PropertFile.class.getClassLoader().getResource(filepath).toString().replace("%20", " ").substring(6);
            File file = new File(confPath);  
            if (!file.exists())  
                file.createNewFile();  
            fis = new FileInputStream(file);  
            prop = new Properties(); 
            prop.load(fis);  
            
            fis.close();           
            fos = new FileOutputStream(confPath);             
            prop.setProperty(key, value);  
            prop.store(fos, "Update '" + key + "' value");      
            fos.close();  
        } catch (IOException e) {  
        	loger.error("Visit " + filepath + " for updating " + value + " value error");  
        }finally{
        	try { 
	            if (fis != null) {   
	            	fis.close();
                }
	            if (fos != null) {   
	            	fos.close();
                }
            } catch (IOException e) {   
                loger.error("Close the stream fails:"+e);   
            }    
        }
    }
}
